// api/modules/hardware.js - 硬件设备模块API
import request from '../index'
import { getBaseUrl } from '@/utils/address'

export default {
  // 设备注册
  registerDevice(data) {
    // 确保必须的字段存在
    if (!data.productId) {
      console.error('registerDevice: productId是必须的')
      return Promise.reject(new Error('产品ID不能为空'))
    }
    if (!data.macAddress) {
      console.error('registerDevice: macAddress是必须的')
      return Promise.reject(new Error('MAC地址不能为空'))
    }
    
    return request.post('/hardware/registration', data)
  },
  
  // 获取设备状态
  getDeviceStatus(deviceId) {
    return request.get(`/hardware/status/${deviceId}`)
  },
  
  // 获取设备固件版本
  getFirmwareVersion(deviceId) {
    return request.get(`/hardware/firmware/${deviceId}`)
  },
  
  // 检查设备固件更新
  checkFirmwareUpdate(deviceId) {
    return request.get(`/hardware/firmware/check/${deviceId}`)
  },
  
  // 触发设备固件更新
  updateFirmware(deviceId) {
    return request.post(`/hardware/firmware/update/${deviceId}`)
  },
  
  // 获取设备网络信息
  getNetworkInfo(deviceId) {
    return request.get(`/hardware/network/${deviceId}`)
  },
  
  // 设备重启
  restartDevice(deviceId) {
    return request.post(`/hardware/restart/${deviceId}`)
  },
  
  // 设备重置
  resetDevice(deviceId) {
    return request.post(`/hardware/reset/${deviceId}`)
  },
  
  // 获取设备日志
  getDeviceLogs(deviceId, params) {
    return request.get(`/hardware/logs/${deviceId}`, params)
  },
  
  // 获取设备配置
  getDeviceConfig(deviceId) {
    return request.get(`/hardware/config/${deviceId}`)
  },
  
  // 更新硬件设备
  updateHardwareDevice(deviceId, data) {
    if (!deviceId) {
      console.error('updateHardwareDevice: 设备ID是必须的')
      return Promise.reject(new Error('设备ID不能为空'))
    }
    
    // 移除 null 或 undefined 的字段
    const cleanedData = Object.fromEntries(
      Object.entries(data).filter(([_, v]) => v !== null && v !== undefined)
    )
    
    return request.put(`/hardware/device/${deviceId}`, cleanedData)
  },
  
  // 绑定设备到用户
  bindDevice(data) {
    return request.post('/hardware/bind', data)
  },
  
  // 解绑设备
  unbindDevice(deviceId) {
    return request.delete(`/hardware/bind/${deviceId}`)
  },
  
  // 获取设备绑定信息
  getBindInfo(deviceId) {
    return request.get(`/hardware/bind/${deviceId}`)
  },
  
  // 检查设备MAC地址是否已注册
  checkMacAddress(macAddress) {
    if (!macAddress) {
      console.error('checkMacAddress: macAddress是必须的')
      return Promise.reject(new Error('MAC地址不能为空'))
    }
    
    // 修改为与后端匹配的路径格式
    return request.get(`/hardware/registration/check/${macAddress}`)
  },
  
  // 获取设备连接历史
  getConnectionHistory(deviceId, params) {
    return request.get(`/hardware/connection-history/${deviceId}`, {
      params
    })
  },
  
  // 获取设备使用统计
  getDeviceStats(deviceId) {
    return request.get(`/hardware/stats/${deviceId}`)
  },
  
  // 获取设备类型列表
  getDeviceTypes() {
    return request.get('/hardware/types')
  },
  
  // 获取固件版本列表
  getFirmwareList() {
    return request.get('/hardware/firmware/list')
  },
  
  
 // 通过MAC地址获取设备
 getDeviceByMac(macAddress) {
   return request.get(`/hardware/by-mac/${macAddress}`);
 },
 
 // 获取当前用户的设备
 getUserDevices(userId) {
   return request.get('/hardware/user', { params: { userId } });
 },

// 发送MQTT消息
sendMqttMessage(payload) {
  // 确保payload不为空
  if (!payload) {
    console.error('sendMqttMessage: payload是必须的');
    return Promise.reject(new Error('消息内容不能为空'));
  }
  
  // 如果payload不是字符串，转换为JSON字符串
  const payloadStr = typeof payload === 'string' ? payload : JSON.stringify(payload);
  
  // 发送请求，使用request.post方法
  return request.post('/hardware/mqtt/send', {
    payload: payloadStr
  });
},

// 配置设备智能体
configureDeviceAI(macAddress, smartDeviceId) {
  if (!macAddress || !smartDeviceId) {
    console.error('configureDeviceAI: 参数不能为空');
    return Promise.reject(new Error('MAC地址和智能体ID不能为空'));
  }
  
  return request.post(`/hardware/ai/configure/${macAddress}`, {
    smartDeviceId: smartDeviceId  // 将smartDeviceId放入请求体中
  });
},

// 清除设备智能体配置
clearDeviceAI(macAddress) {
  if (!macAddress) {
    console.error('clearDeviceAI: MAC地址不能为空');
    return Promise.reject(new Error('MAC地址不能为空'));
  }
  
  return request.post(`/hardware/ai/clear/${macAddress}`);
},

// 获取设备当前智能体配置
getCurrentDeviceAI(macAddress) {
  if (!macAddress) {
    console.error('getCurrentDeviceAI: MAC地址不能为空');
    return Promise.reject(new Error('MAC地址不能为空'));
  }
  
  return request.get(`/hardware/ai/current/${macAddress}`);
},
  
  // 设备远程控制 - 发送命令
  sendCommand(deviceId, command) {
    return request.post(`/hardware/command/${deviceId}`, { command })
  },
  
  
  // 新增硬件设备
    createHardwareDevice(data) {
      if (!data.nickname) {
        console.error('createHardwareDevice: 设备名称是必须的')
        return Promise.reject(new Error('设备名称不能为空'))
      }
      
      return request.post('/hardware/device', data)
    },
  
    // 更新硬件设备
    updateHardwareDevice(deviceId, data) {
      if (!deviceId) {
        console.error('updateHardwareDevice: 设备ID是必须的')
        return Promise.reject(new Error('设备ID不能为空'))
      }
      
      return request.put(`/hardware/device/${deviceId}`, data)
    },
  
    // 删除硬件设备
    deleteHardwareDevice(deviceId) {
      if (!deviceId) {
        console.error('deleteHardwareDevice: 设备ID是必须的')
        return Promise.reject(new Error('设备ID不能为空'))
      }
      
      return request.delete(`/hardware/device/${deviceId}`)
    }
}